package com.david.equipment.controller;

import com.david.common.utils.PageResult;
import com.david.equipment.pojo.entity.*;
import com.david.equipment.pojo.vo.*;
import com.david.equipment.service.TacticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/equipment/tactics")
@Api(tags = "设备管理-设备运维")
@Tag(name = "TacticsController", description = "设备运维")
@Slf4j
public class TacticsController {
    @Autowired
    private TacticsService tacticsService;

    /**
    * @Description: 查询部门已完成或过期任务
    * @Param: [deptId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/9/10
    */
    @ApiOperation(value = "查询部门已完成或过期任务")
    @GetMapping("/gethistask/{deptId}")
    public ResponseEntity<Object> getHisTask(@PathVariable Integer deptId){
        log.info("获取部门{}的已结束任务",deptId);
        TacticsHisTaskEntity hisTask = TacticsHisTaskEntity.builder().deptId(deptId).build();
        List<TacticsHisTaskVO> tacticsHisTaskVOS = tacticsService.getHisTask(hisTask);
        return success(true,tacticsHisTaskVOS);
    }

    /**
    * @Description: 新增策略
    * @Param: [tacticsVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/9/10
    */
    @PutMapping("/add")
    @ApiOperation("新增策略")
    public ResponseEntity<Object> addTactics(@RequestBody TacticsVO tacticsVO){
        log.info("添加策略{}",tacticsVO);
        System.out.println(tacticsVO.getName());
        System.out.println(tacticsVO.getDevicelist());
        tacticsService.addTactics(tacticsVO);
        return success(true, "添加成功");
    }

    @PutMapping("update")
    public ResponseEntity<Object> updateTactics(@RequestBody TacticsVO tacticsVO){
        log.info("修改策略{}",tacticsVO);
        tacticsService.updateTactics(tacticsVO);
        return success(true, "修改成功");
    }

    @ApiOperation("根据类型查询所有策略")
    @PostMapping("/{type}")
    public ResponseEntity<Object> getTactics(@RequestBody CommonVO commonVO, @PathVariable String type){
        log.info("获取所有{}策略{}",type,commonVO);
        return success(true,tacticsService.getTactics(commonVO,type));
    }

    @PostMapping("/search/{type}")
    public ResponseEntity<Object> getTacticsBysearch(@RequestBody TacticsSearchVO tacticsSearchVO,@PathVariable String type){
        log.info("根据类型{}搜索策略{}",type,tacticsSearchVO);
        PageResult tacticsBySearch = tacticsService.getTActicsBySearch(tacticsSearchVO, type);
        return success(true,tacticsBySearch);
    }

    @ApiOperation("新增项目组")
    @PutMapping("/addProject")
    public ResponseEntity<Object> addProject(@RequestBody TacticsProjectVO tacticsProjectVO){
        log.info("添加项目{}",tacticsProjectVO);
        tacticsService.addProject(tacticsProjectVO);
        return success(true, "上传成功");
    }

    @ApiOperation("查询项目组（类型和部门ID）")
    @PostMapping("/getProject")
    public ResponseEntity<Object> getProject(@RequestParam(required = false,name="type") String type, @RequestParam(required = false,name= "deptId") Integer deptId){
        log.info("获取部门{}的{}类型项目",deptId,type);
        List<TacticsProjectVO> projectVOList = tacticsService.getProject(type,deptId);
        return success(true,projectVOList);
    }

    @ApiOperation(value = "编辑项目组")
    @PostMapping("/editProject")
    public ResponseEntity<Object> editProject(@RequestBody TacticsProjectVO tacticsProjectVO){
        log.info("编辑项目{}",tacticsProjectVO);
        tacticsService.editProject(tacticsProjectVO);
        return success(true,"编辑成功");
    }

    @ApiOperation(value = "删除项目组")
    @DeleteMapping("/delProject/{id}")
    public ResponseEntity<Object> delProject(@PathVariable Integer id){
        log.info("删除项目组{}",id);
        Boolean result = tacticsService.delProject(id);
        if (result) {
            return success(true,"删除成功");
        } else {
            return success(false,"项目正在使用中，无法删除");
        }
    }
    
    @ApiOperation(value = "获取已完成及待办任务")
    @GetMapping("/getTask")
    public ResponseEntity<Object> getNowTask(){
        log.info("获取已完成及待办任务");
        List<TacticsNowTaskVO> tasknow = tacticsService.getNowTask();
//        String tasknow = tacticsService.getNowTask().get(1).getType();
        return success(true,tasknow);
    }

    @ApiOperation(value = "查询当前任务检查项目")
    @PostMapping("/getItems")
    public ResponseEntity<Object> getTaskItems(@RequestParam("taskId") Integer taskId, @RequestParam("deviceId") Integer deviceId){
        log.info("获取任务{}的设备{}的检查明细",taskId,deviceId);
        List<TacticsTaskItems> taskItems = tacticsService.getNowTaskItems(taskId,deviceId);
        return success(true,taskItems);
    }

    @ApiOperation(value="发送检查明细结果")
    @PostMapping("/sendHisItems")
    public ResponseEntity<Object> sendHisItems(@RequestBody List<TacticsHisTaskItems> itemsList){
        log.info("发送检查明细结果{}", itemsList);
        tacticsService.sendHisItems(itemsList);
        return success(true,"任务已完成");
    }

    @ApiOperation(value="修改任务状态")
    @PostMapping("/changeStatus/{ids}")
    public ResponseEntity<Object> changeStatus(@PathVariable List<Integer> ids, @RequestParam("status") String status){
        log.info("修改{}任务状态{}",ids,status);
        tacticsService.changeStatus(ids,status);
        return success(true,"状态修改完成");
    }

    @ApiOperation(value = "获取策略对应设备")
    @GetMapping("/getttDevices/{tacticsId}")
    public ResponseEntity<Object> getTacticsDevices(@PathVariable Integer tacticsId){
        log.info("获取策略{}设备",tacticsId);
        return success(true,tacticsService.getTacticsDevices(tacticsId));
    }

    @ApiOperation(value = "获取项目对应items")
    @GetMapping("/getttProject/{projectId}")
    public ResponseEntity<Object> getTacticsProject(@PathVariable Integer projectId){
        log.info("获取项目{}的items信息",projectId);
        return success(true,tacticsService.getProjectItems(projectId));
    }

    @ApiOperation(value = "删除策略")
    @DeleteMapping("/del/{tacticsIds}")
    public ResponseEntity<Object> del(@PathVariable List<Integer> tacticsIds){
        log.info("删除{}策略",tacticsIds);
        tacticsService.delTacticsByIds(tacticsIds);
        return success(true,"删除成功");
    }
}
